﻿namespace PNunitToolsClass
{
    using System;
    using System.IO;
    using System.Xml.Serialization;

    public class LauncherConfig
    {
        public TestGroup Config;

        public LauncherConfig(TestGroup config)
        {
            this.Config = config;
        }

        public static LauncherConfig Load(string filename)
        {
            Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            return Load(stream);
        }

        public static LauncherConfig Load(Stream fileStream)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(TestGroup));
            TestGroup config = (TestGroup)serializer.Deserialize(fileStream);
            return new LauncherConfig(config);
        }

        public void Save(string filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(TestGroup));
            FileStream stream = File.Create(filename);
            serializer.Serialize(stream, this.Config);
            stream.Close();
        }

        public class Names
        {
            public const string PNUnitAgentServiceName = "IPNUnitAgent";
            public const string ServerBarrier = "SERVERSTART";
            public const string EndBarrier = "ENDBARRIER";
            public const string RestartBarrier = "SERVERRESTART";
            public const string RestartedOkBarrier = "SERVERRESTARTEDOK";
        }

        [Serializable]
        public class TestGroup
        {
            public Variable[] Variables;
            public ParallelTest[] ParallelTests;            
            public string LauncherMachine;
            public string ResultFileName;
            public string[] MachineAlias;

            public TestGroup()
            {
            }
        }

        public class Variable
        {
            [XmlAttribute]
            public string Name;

            [XmlAttribute]
            public string Value;

            public override string ToString()
            {
                return string.Format("[{0}]=[{1}]", this.Name, this.Value);
            }

            public Variable()
            {
            }

            public Variable(string name, string value)
            {
                this.Name = name;
                this.Value = value;
            }

            public Variable(string variable)
            {
                this.Name = variable.Split('=')[0].Replace("[", string.Empty).Replace("]", string.Empty);
                this.Value = variable.Split('=')[1].Replace("[", string.Empty).Replace("]", string.Empty);
            }
        }

        [Serializable]
        public class ParallelTest
        {
            public string Name;
            public string[] Agents;
            public int ParalletTestTimeout;
            public int BarrierTimeout;
            public TestConf[] Tests;
        }

        [Serializable]
        public class TestConf
        {
            public string Name;
            public string Assembly;
            public string TestToRun;
            public string Machine;
            public string[] TestParams;
            public string StartBarrier = Names.ServerBarrier;
            public string EndBarrier = Names.EndBarrier;
            public string[] WaitBarriers;
        }
    }
}
